package edu.fourtwelve.srlzd_data;

import java.io.Serializable;

/**
 *
 * @author Christopher C. Hunt
 */
public class MemberData implements Serializable {

    private static final long serialVersionUID = -8373896860781794494L;
    
    private transient Object CHAR_SYNCH;
    private volatile String usrName;
    private volatile String fName;
    private volatile String lName;
    private volatile String address;
    private volatile String address2;
    private volatile String city;
    private volatile String state;
    private volatile String primaryAccount;
    private byte[] salt;
    private byte[] password;
    private int passLength;

    public MemberData(String usrName) {
        CHAR_SYNCH = new Object();
        this.usrName = usrName;
    }

    public MemberData copy(MemberData data) {
        MemberData ret = new MemberData(data.getUsrName());

        ret.setFName(data.getFName());
        ret.setLName(data.getLName());
        ret.setAddress(data.getAddress());
        ret.setAddress2(data.getAddress2());
        ret.setCity(data.getCity());
        ret.setState(data.getState());
        ret.setPrimaryAccount(data.getPrimaryAccount());
        ret.setPassword(data.getPassword());

        return ret;
    }

    public String getUsrName() {
        return usrName;
    }

    public String getFName() {
        return fName;
    }

    public String getLName() {
        return lName;
    }

    public String getAddress() {
        return address;
    }

    public String getAddress2() {
        return address2;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
        return state;
    }

    public String getPrimaryAccount() {
        return primaryAccount;
    }

//    public void setUsrName(String usrName) {
//        this.usrName = usrName;
//    }
    public void setFName(String fName) {
        this.fName = fName;
    }

    public void setLName(String lName) {
        this.lName = lName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setPrimaryAccount(String primaryAccount) {
        this.primaryAccount = primaryAccount;
    }

    public byte[] getPassword() {
        synchronized (CHAR_SYNCH) {
            return this.password;
        }
    }

    public byte[] getSalt() {
        synchronized (CHAR_SYNCH) {
            return this.salt;
        }
    }
    
    public void insertIntoPassword(int index, byte ch) {
        if (index <= passLength && index > -1) {
            synchronized (CHAR_SYNCH) {
                if (index == passLength) {
                    byte[] tmp = new byte[passLength + 1];
                    System.arraycopy(this.password, 0, tmp, 0, passLength);
                    tmp[passLength] = ch;
                    this.password = tmp;
                    passLength++;
                } else {
                    this.password[index] = ch;
                }
            }
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

    public void setPassword(byte[] newCh) {
        synchronized (CHAR_SYNCH) {
            this.password = new byte [newCh.length];
            System.arraycopy(newCh, 0, this.password, 0, newCh.length);
            this.passLength = this.password.length;
        }
    }
    
    public void setSalt(byte[] newCh) {
        synchronized (CHAR_SYNCH) {
            this.salt = new byte [newCh.length];
            System.arraycopy(newCh, 0, this.salt, 0, newCh.length);
        }
    }
    
    private void writeObject(java.io.ObjectOutputStream stream)
        throws java.io.IOException
    {
        stream.defaultWriteObject();
    }

    private void readObject(java.io.ObjectInputStream stream)
        throws java.io.IOException, ClassNotFoundException
    {
        stream.defaultReadObject();
        this.CHAR_SYNCH = new Object();
    }
    
    public boolean validateForInsert() {
        return (this.getUsrName() != null) && (this.getFName() != null)
                    && (this.getLName() != null) && (this.getAddress() != null)
                    && (this.getCity() != null) && (this.getState() != null)
                    && (this.getPrimaryAccount() != null) && (this.getPassword() != null)
                    && (!(this.getUsrName().equals("") || this.getFName().equals("")
                    || this.getLName().equals("") || this.getAddress().equals("")
                    || this.getCity().equals("") || this.getState().equals("")
                    || this.getPrimaryAccount().equals("")));
    }
}
